package com.sskj.mine.ui.activity;


import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.bulong.rudeness.RudenessScreenHelper;
import com.shizhefei.mvc.MVCCoolHelper;
import com.shizhefei.view.coolrefreshview.CoolRefreshView;
import com.sskj.common.adapter.slimadapter.IViewInjector;
import com.sskj.common.adapter.slimadapter.SlimAdapter;
import com.sskj.common.adapter.slimadapter.SlimInjector;
import com.sskj.common.base.App;
import com.sskj.common.box.decoration.DividerLineItemDecoration;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.lib.mvchelper.ModelRx2DataSource;
import com.sskj.mine.R;
import com.sskj.mine.R2;
import com.sskj.mine.bean.ClientBean;
import com.sskj.mine.presenter.MyClientActivityPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

@Route(path = RConfig.MINE_MY_CLIENT)//我的客户
public class MyClientActivity extends BaseActivity<MyClientActivityPresenter> {
    @BindView(R2.id.recyclerView)
    RecyclerView recyclerView;
    @BindView(R2.id.coolRefreshView)
    CoolRefreshView coolRefreshView;

    @Override
    protected int getLayoutResId() {
        return R.layout.mine_activity_my_client;
    }

    @Override
    public MyClientActivityPresenter getPresenter() {
        return new MyClientActivityPresenter();
    }

    @Override
    protected void initView() {
        setTitle(getString(R.string.mine_client));
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerLineItemDecoration(this)
                .setPaintWidth((int) RudenessScreenHelper.pt2px(App.INSTANCE, 1))
                .setDividerColor(ContextCompat.getColor(App.INSTANCE, R.color.transparent))
        );
        SlimAdapter slimAdapter = SlimAdapter.create().register(R.layout.mine_recy_item_my_client, new SlimInjector<ClientBean>() {
            @Override
            public void onInject(ClientBean data, IViewInjector injector, List payloads) {
                injector
                        .text(R.id.tvName, data.getUsername())
                        .text(R.id.tvPhone, TextUtils.isEmpty(data.getTel()) ? data.getEmail() : data.getTel())
                        .text(R.id.tvTime, data.getCreateTime())
                ;
            }
        }).attachTo(recyclerView).updateData(new ArrayList());
        ModelRx2DataSource<ClientBean> dataSource = new ModelRx2DataSource<>((ModelRx2DataSource.OnLoadSource<ClientBean>)
                page -> mPresenter.getClient(page), 20);
        MVCCoolHelper<List<ClientBean>> mvcHelper = new MVCCoolHelper<>(coolRefreshView);
        mvcHelper.setDataSource(dataSource);
        mvcHelper.setAdapter(slimAdapter);
        mvcHelper.refresh();
    }

    @Override
    public boolean getImmersion() {
        return false;
    }
}
